﻿using IdentityModel.Client;
using System;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace TestConsoleApp1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var regex = new Regex(".*$");
            //var b1 = regex.IsMatch(null);

            var p = "1234567890";
            var a = p.Substring(0, 2);
            var b = p.Substring(2, 4);
            var c = p.Substring(6, p.Length - 6);
            Console.WriteLine(Path.Combine(a, b, c));
          
            Console.WriteLine(1.ToString("000"));
            Console.WriteLine("Hello World!");
            var handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, chainName) => true;
            var client = new HttpClient(handler);
            var doc = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
            {
                Address = "https://tj-sso.50yc.cn",
                Policy = new DiscoveryPolicy
                {
                    RequireHttps = false
                }
            });

            var tokenClient = new TokenClient(client, 
                new TokenClientOptions() { 
                Address = doc.TokenEndpoint,
                ClientId = "m2m",
                ClientSecret = "secret"
                });
            var tokenResponse = await tokenClient.RequestClientCredentialsTokenAsync("api");
            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }
            Console.WriteLine(tokenResponse.Json);

            client = new HttpClient();
            client.SetBearerToken(tokenResponse.AccessToken);
            var response = await client.GetAsync("https://localhost:7104/identity");
            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();
                Console.WriteLine(content);
            }

            //----------------------
            tokenClient = new TokenClient(client,
              new TokenClientOptions()
              {
                  Address = doc.TokenEndpoint,
                  ClientId = "ro.client",
                  ClientSecret = "secret"
              });
            tokenResponse = await tokenClient.RequestPasswordTokenAsync("ailisi", "password", "api");
            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }
            Console.WriteLine(tokenResponse.Json);

            client = new HttpClient();
            client.SetBearerToken(tokenResponse.AccessToken);
            response = await client.GetAsync("https://localhost:7104/identity");
            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();
                Console.WriteLine(content);
            }

        }
    }
}
